﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Common;
using System.Data.OleDb;
using System.Text.RegularExpressions;

namespace AliCMS.CourseModule.OleDb
{
    public class CourseStudy : OleDbBase, Dal.ICourseStudy
    {
        public int GetInnerIDFromGuid(Guid guid)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.CourseStudy.ID, MapType(Alias.CourseStudy.ID)).Value = guid;
            Command.CommandText = "SELECT " + MapColumn(Alias.CourseStudy.InnerID) + " FROM " + MapTable(Alias.Objects.CourseStudy) + " WHERE " + MapColumn(Alias.CourseStudy.ID) + "=?";
            object result = Command.ExecuteScalar();
            if (result == null)
                return 0;
            else
                return (int)result;
        }
        public Guid GetGuidFromInnerID(object id)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.CourseStudy.InnerID, MapType(Alias.CourseStudy.InnerID)).Value = id;
            Command.CommandText = "SELECT " + MapColumn(Alias.CourseStudy.ID) + " FROM " + MapTable(Alias.Objects.CourseStudy) + " WHERE " + MapColumn(Alias.CourseStudy.InnerID) + "=?";
            object result = Command.ExecuteScalar();
            if (result == null)
                return Guid.Empty;
            else
                return (Guid)result;
        }
        public Dictionary<string, object> Get(object id)
        {
            Dictionary<string, object> result = new Dictionary<string, object>();

            StringBuilder sql = new StringBuilder("SELECT TOP 1");
            sql.Append(" " + MapColumn(Alias.CourseStudy.ID));
            sql.Append("," + MapColumn(Alias.CourseStudy.InnerID));
			sql.Append("," + MapColumn(Alias.CourseStudy.UserID));
			sql.Append("," + MapColumn(Alias.CourseStudy.CourseID));
			sql.Append("," + MapColumn(Alias.CourseStudy.CourseName));
			sql.Append("," + MapColumn(Alias.CourseStudy.CourseThumbnail));
			sql.Append("," + MapColumn(Alias.CourseStudy.Max));
			sql.Append("," + MapColumn(Alias.CourseStudy.Value));
			sql.Append("," + MapColumn(Alias.CourseStudy.StartTime));
			sql.Append("," + MapColumn(Alias.CourseStudy.EndTime));
			sql.Append("," + MapColumn(Alias.CourseStudy.LastLessonID));
            sql.Append(" FROM " + MapTable(Alias.Objects.CourseStudy));

            if (id is Guid)
            {
                sql.Append(" WHERE " + MapColumn(Alias.CourseStudy.ID) + "=?");

                Command.Parameters.Clear();
                Command.Parameters.Add("@" + Alias.CourseStudy.ID, MapType(Alias.CourseStudy.ID)).Value = id;
            }
            else
            {
                sql.Append(" WHERE " + MapColumn(Alias.CourseStudy.InnerID) + "=?");

                Command.Parameters.Clear();
                Command.Parameters.Add("@" + Alias.CourseStudy.InnerID, MapType(Alias.CourseStudy.InnerID)).Value = id;
            }

            Command.CommandText = sql.ToString();
            DataReader = Command.ExecuteReader();
            if (!DataReader.HasRows)
            {
                DataReader.Close();
                return null;
            }

            DataReader.Read();
            int i = 0;
            result[Alias.CourseStudy.ID] = DataReader[i++];
            result[Alias.CourseStudy.InnerID] = DataReader[i++];
			result[Alias.CourseStudy.UserID] = DataReader[i++];
			result[Alias.CourseStudy.CourseID] = DataReader[i++];
			result[Alias.CourseStudy.CourseName] = DataReader[i++];
			result[Alias.CourseStudy.CourseThumbnail] = DataReader[i++];
			result[Alias.CourseStudy.Max] = DataReader[i++];
			result[Alias.CourseStudy.Value] = DataReader[i++];
			result[Alias.CourseStudy.StartTime] = DataReader[i++];
			result[Alias.CourseStudy.EndTime] = DataReader[i++];
			result[Alias.CourseStudy.LastLessonID] = DataReader[i++];
            DataReader.Close();

            return result;
        }
        public void Add(Dictionary<string, object> item)
        {
            #region 设置RowLang
            item.Add(Alias.CourseStudy.RowLang, "|" + Lang + "|");
            #endregion

            StringBuilder names = new StringBuilder();
            StringBuilder values = new StringBuilder();

            Command.Parameters.Clear();
            PrepareParameters(names, values, item);

            Command.CommandText = "INSERT INTO " + MapTable(Alias.Objects.CourseStudy) + "(" + names + ") VALUES(" + values + ")";
            Command.ExecuteNonQuery();
        }
        public void Modify(int id, Dictionary<string, object> changes)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.CourseStudy.InnerID, MapType(Alias.CourseStudy.InnerID)).Value = id;

            #region 检查并设置RowLang
            Command.CommandText = "SELECT " + MapColumn(Alias.CourseStudy.RowLang) + " FROM " + MapTable(Alias.Objects.CourseStudy) + " WHERE " + MapColumn(Alias.CourseStudy.InnerID) + "=?";
            string rowLang = Command.ExecuteScalar().ToString();
            if (!rowLang.Contains("|" + Lang + "|"))
                changes.Add(Alias.CourseStudy.RowLang, rowLang + Lang + "|");
            #endregion

            StringBuilder sql = new StringBuilder("UPDATE " + MapTable(Alias.Objects.CourseStudy) + " SET ");
            Command.Parameters.Clear();
            PrepareParameters(sql, changes);
            Command.Parameters.Add("@" + Alias.CourseStudy.InnerID, MapType(Alias.CourseStudy.InnerID)).Value = id;

            sql.Append(" WHERE " + MapColumn(Alias.CourseStudy.InnerID) + "=?");

            Command.CommandText = sql.ToString();
            Command.ExecuteNonQuery();
        }
        public void Delete(int id)
        {
            Command.Parameters.Clear();
            Command.Parameters.Add("@" + Alias.CourseStudy.InnerID, MapType(Alias.CourseStudy.InnerID)).Value = id;

            StringBuilder sql = new StringBuilder("DELETE FROM " + MapTable(Alias.Objects.CourseStudy) + " WHERE " + MapColumn(Alias.CourseStudy.InnerID) + "=?");

            Command.CommandText = sql.ToString();
            Command.ExecuteNonQuery();
        }
        public DataTable List(int page, int pageSize, string condition, string sort)
        {
            DataTable result = new DataTable();
            result.Columns.Add(Alias.CourseStudy.ID, MapCSType(Alias.CourseStudy.ID));
            result.Columns.Add(Alias.CourseStudy.InnerID, MapCSType(Alias.CourseStudy.InnerID));
			result.Columns.Add(Alias.CourseStudy.UserID, MapCSType(Alias.CourseStudy.UserID));
			result.Columns.Add(Alias.CourseStudy.CourseID, MapCSType(Alias.CourseStudy.CourseID));
			result.Columns.Add(Alias.CourseStudy.CourseName, MapCSType(Alias.CourseStudy.CourseName));
			result.Columns.Add(Alias.CourseStudy.CourseThumbnail, MapCSType(Alias.CourseStudy.CourseThumbnail));
			result.Columns.Add(Alias.CourseStudy.Max, MapCSType(Alias.CourseStudy.Max));
			result.Columns.Add(Alias.CourseStudy.Value, MapCSType(Alias.CourseStudy.Value));
			result.Columns.Add(Alias.CourseStudy.StartTime, MapCSType(Alias.CourseStudy.StartTime));
			result.Columns.Add(Alias.CourseStudy.EndTime, MapCSType(Alias.CourseStudy.EndTime));
			result.Columns.Add(Alias.CourseStudy.LastLessonID, MapCSType(Alias.CourseStudy.LastLessonID));

            #region 条件
            Command.Parameters.Clear();
            StringBuilder where = new StringBuilder();
            if (!String.IsNullOrEmpty(condition))
            {
                where.Append(" WHERE " + Alias.CourseStudy.RowLang + " LIKE '%|" + Lang + "|%' AND ");
                where.Append("(" + condition + ")");

                ParseCondition(where, "");
            }
            else
            {
                where.Append(" WHERE " + MapColumn(Alias.CourseStudy.RowLang) + " LIKE '%|" + Lang + "|%'");
            }

            #endregion

            #region 排序
            if (!String.IsNullOrEmpty(sort))
                sort = " ORDER BY " + ParseSort(sort, "");
            else
                sort = " ORDER BY " + MapColumn(Alias.CourseStudy.InnerID) + " DESC";
            #endregion

            #region 构造SQL语句
            StringBuilder sql = new StringBuilder("SELECT");
            if (page > 0 && pageSize > 0)
                sql.Append(" TOP " + (page * pageSize));
            sql.Append(" " + MapColumn(Alias.CourseStudy.ID));
            sql.Append("," + MapColumn(Alias.CourseStudy.InnerID));
			sql.Append("," + MapColumn(Alias.CourseStudy.UserID));
			sql.Append("," + MapColumn(Alias.CourseStudy.CourseID));
			sql.Append("," + MapColumn(Alias.CourseStudy.CourseName));
			sql.Append("," + MapColumn(Alias.CourseStudy.CourseThumbnail));
			sql.Append("," + MapColumn(Alias.CourseStudy.Max));
			sql.Append("," + MapColumn(Alias.CourseStudy.Value));
			sql.Append("," + MapColumn(Alias.CourseStudy.StartTime));
			sql.Append("," + MapColumn(Alias.CourseStudy.EndTime));
			sql.Append("," + MapColumn(Alias.CourseStudy.LastLessonID));
            sql.Append(" FROM " + MapTable(Alias.Objects.CourseStudy));
            sql.Append(where + sort);
            Command.CommandText = sql.ToString();
            #endregion

            #region 执行SQL语句并获取记录
            int curIndex = 0, rowIndex = 0;
            int startIndex = (page - 1) * pageSize;
            DataReader = Command.ExecuteReader();
            while (DataReader.Read())
            {
                if (pageSize != -1)
                {
                    if (curIndex < startIndex)
                    {
                        curIndex++;
                        continue;
                    }

                    if (rowIndex == pageSize)
                        break;
                }

                DataRow row = result.NewRow();
                int i = 0;
                row[Alias.CourseStudy.ID] = DataReader[i++];
                row[Alias.CourseStudy.InnerID] = DataReader[i++];
				row[Alias.CourseStudy.UserID] = DataReader[i++];
				row[Alias.CourseStudy.CourseID] = DataReader[i++];
				row[Alias.CourseStudy.CourseName] = DataReader[i++];
				row[Alias.CourseStudy.CourseThumbnail] = DataReader[i++];
				row[Alias.CourseStudy.Max] = DataReader[i++];
				row[Alias.CourseStudy.Value] = DataReader[i++];
				row[Alias.CourseStudy.StartTime] = DataReader[i++];
				row[Alias.CourseStudy.EndTime] = DataReader[i++];
				row[Alias.CourseStudy.LastLessonID] = DataReader[i++];
                result.Rows.Add(row);

                rowIndex++;
            }
            DataReader.Close();
            #endregion

            return result;
        }
        public int GetRecordCount(string condition)
        {
			#region 条件
			Command.Parameters.Clear();
			StringBuilder where = new StringBuilder();
			if (!String.IsNullOrEmpty(condition))
			{
				where.Append(" WHERE " + Alias.CourseStudy.RowLang + " LIKE '%|" + Lang + "|%' AND ");
				where.Append("(" + condition + ")");

				ParseCondition(where, "");
			}
			else
			{
				where.Append(" WHERE " + MapColumn(Alias.CourseStudy.RowLang) + " LIKE '%|" + Lang + "|%'");
			}

			#endregion

            Command.CommandText = "SELECT COUNT(*) FROM " + MapTable(Alias.Objects.CourseStudy) + where;
            return Convert.ToInt32(Command.ExecuteScalar());
        }
    }
}
